package com.example;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;

public final class Config {
    public InetAddress address;
    public int port;
    private final String path;

    Config(InetAddress address, short port, String path) {
        this.address = address;
        this.port = port;
        this.path = path;
    }

    Config(InetAddress address, short port) {
        this.address = address;
        this.port = port;
        this.path = null;
    }

    public Optional<String> path() {
        return Optional.ofNullable(path);
    }

    public static Config parseArgs(String[] args) {
        Options options = new Options();

        options.addOption("h", "help", false, "print this message");

        Option path = Option.builder("p").longOpt("path").hasArg().argName("path")
                .desc("the path of the file to be get").build();

        options.addOption(path);

//        for (String string : args) {
//            System.out.println(string);
//        }

        CommandLineParser parser = new DefaultParser();

        try {
            CommandLine cmd = parser.parse(options, args);
            // for (String string : cmd.getArgs()) {
            // System.out.println(string);
            // }

            HelpFormatter formatter = new HelpFormatter();

            // 如果有 -h 参数，打印帮助信息
            if (cmd.hasOption("h")) {
                formatter.printHelp("mytelnet <HOST> <port>", options, true);
                System.exit(0);
            }

            var argsArray = cmd.getArgs();

            InetAddress address = null;
            short port = 0;
            String pathStr = null;
            Config config = null;

            if (argsArray.length == 2) {
                address = InetAddress.getByName(argsArray[0]);
                port = Short.parseShort(argsArray[1]);
            } else {
                System.err.printf("Invalid arguments numbers: %d%n", argsArray.length);
                formatter.printHelp("mytelnet <HOST> <port>", options, true);
                System.exit(1);
            }

            if (cmd.hasOption("p")) {
                pathStr = cmd.getOptionValue("p");
                config = new Config(address, port, pathStr);
            } else {
                config = new Config(address, port);
            }

            return config;

        } catch (ParseException exp) {
            System.err.printf("Parsing failed.  Reason: %s%n", exp.getMessage());
            System.exit(1);

        }catch (NumberFormatException e) {
            System.err.println("Parsing port number failed.");
            System.exit(1);

        } catch (Exception e) {
            System.err.println("An unexpected error occurred.");
            System.exit(1);
        }
        return null; // 永远不会执行到这。但是javac不知道，所以要加上，否则过不了编译。
    }

}